﻿Imports DTO
Imports BUS
Imports System.Data.SqlClient
Imports System.Drawing.Printing.PrintDocument
Imports System.Drawing.Printing

Public Class frmTraCuuPin
    Private list As New List(Of PinDTO)
    Private pin As New PinDTO
    Private pinController As New PinBUS
    Private listPin As New List(Of PinDTO)
    Public sListPinSelect As New List(Of Integer)
    Public dataPin As New DataTable

    Private Sub LoadGrid()
        'dgvDuLieuThem.Rows.Clear()
        dgvDuLieuThem.DataSource = list
        For i As Integer = 0 To list.Count - 1
            dgvDuLieuThem.Rows.Add()
            dgvDuLieuThem.Rows.Item(i).Cells(0).Value = list(i).MaPin
            dgvDuLieuThem.Rows.Item(i).Cells(1).Value = list(i).TenPin
            dgvDuLieuThem.Rows.Item(i).Cells(2).Value = list(i).MaLoai
            dgvDuLieuThem.Rows.Item(i).Cells(3).Value = list(i).CongSuat
            dgvDuLieuThem.Rows.Item(i).Cells(4).Value = list(i).KichThuoc
            dgvDuLieuThem.Rows.Item(i).Cells(5).Value = list(i).HangSanXuat
            dgvDuLieuThem.Rows.Item(i).Cells(6).Value = list(i).Imp
            dgvDuLieuThem.Rows.Item(i).Cells(7).Value = list(i).Vmp
            dgvDuLieuThem.Rows.Item(i).Cells(8).Value = list(i).Isc
            dgvDuLieuThem.Rows.Item(i).Cells(9).Value = list(i).Voc
        Next
    End Sub
    Private Sub LoadData()
        Dim ds As List(Of PinDTO) = BUS.PinBUS.ReadData()
        dgvDuLieuThem.DataSource = ds
        list = ds
    End Sub
    Private Sub LoadLoaiPin()
        cbLoaiPin.DataSource = BUS.PinBUS.ReadLoaiPin().Tables("LoaiPin")
        cbLoaiPin.DisplayMember = "TenLoai"
        cbLoaiPin.ValueMember = "MaLoai"
    End Sub
    Private Sub btnTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTatCa.Click
        LoadData()
    End Sub

    Private Sub frmTraCuuPin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadLoaiPin()

        btnSua.Enabled = False
        btnXoa.Enabled = False

        txtImp.Enabled = False
        txtToImp.Enabled = False
        txtVmp.Enabled = False
        txtToVmp.Enabled = False
        txtIsc.Enabled = False
        txtToIsc.Enabled = False
        txtVoc.Enabled = False
        txtToVoc.Enabled = False
        txtCongSuat.Enabled = False
        txtToCongSuat.Enabled = False
    End Sub

    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        'Dim mang As New ArrayList
        'If Not String.IsNullOrEmpty(txtMaPin.Text) Then
        '    mang.Add("MaPin = '" & txtMaPin.Text & "'")
        'End If
        'If Not String.IsNullOrEmpty(txtTenPin.Text) Then
        '    mang.Add("TenPin = '" & txtTenPin.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(cbLoaiPin.Text) Then
        '    mang.Add("MaLoai = '" & cbLoaiPin.SelectedValue & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtImp.Text) Then
        '    mang.Add("CongSuat = '" & txtImp.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtKichThuoc.Text) Then
        '    mang.Add("KichThuoc = '" & txtKichThuoc.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtHangSX.Text) Then
        '    mang.Add("HangSanXuat = '" & txtHangSX.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtImp.Text) Then
        '    mang.Add("Imp = '" & txtImp.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtVmp.Text) Then
        '    mang.Add("Vmp = '" & txtVmp.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtIsc.Text) Then
        '    mang.Add("Isc = '" & txtIsc.Text & "'")
        'End If

        'If Not String.IsNullOrEmpty(txtVoc.Text) Then
        '    mang.Add("Voc = '" & txtKichThuoc.Text & "'")
        'End If

        'Dim dieu_kien As String = ""
        'For i As Integer = 0 To mang.Count - 1
        '    If i = 0 Then
        '        dieu_kien = mang(0)
        '    Else
        '        dieu_kien = dieu_kien & " and " & mang(i)
        '    End If
        'Next

        'Dim bang_Pin As New DataTable
        'bang_Pin = PinBUS.ReadDataTable()
        'bang_Pin.DefaultView.RowFilter = dieu_kien
        'dgvDuLieuThem.DataSource = bang_Pin
        ''Them vao list 
        'For Each row As DataGridViewRow In dgvDuLieuThem.Rows
        '    Dim pin As New PinDTO(row.Cells(0).Value.ToString, row.Cells(1).Value.ToString, row.Cells(2).Value.ToString, row.Cells(3).Value.ToString, row.Cells(4).Value.ToString, row.Cells(5).Value.ToString, row.Cells(6).Value.ToString, row.Cells(7).Value.ToString, row.Cells(8).Value.ToString, row.Cells(9).Value.ToString)
        '    list.Add(pin)
        'Next
        Dim MaPin As Integer = Val(txtMaPin.Text)
        Dim TenPin As String = txtTenPin.Text
        Dim MaLoai As Integer = cbLoaiPin.SelectedValue
        Dim CongSuat As Integer = Val(txtCongSuat.Text)
        Dim KichThuoc As String = txtKichThuoc.Text
        Dim HangSX As String = txtHangSX.Text
        Dim ImpMin As Integer = Val(txtImp.Text)
        Dim ImpMax As Integer = Val(txtToImp.Text)
        Dim VmpMin As Integer = Val(txtVmp.Text)
        Dim VmpMax As Single = Val(txtToVmp.Text)
        Dim IscMin As Integer = Val(txtIsc.Text)
        Dim IscMax As Integer = Val(txtToIsc.Text)
        Dim VocMin As Integer = Val(txtVoc.Text)
        Dim VocMax As Integer = Val(txtToVoc.Text)
        pin = New PinDTO(MaPin, TenPin, MaLoai, CongSuat, KichThuoc, HangSX, ImpMin, ImpMax, VmpMin, VmpMax, IscMin, IscMax, VocMin, VocMax)
        listPin = pinController.TimKiem(pin)
        If listPin.Count() <= 0 Then
            MessageBox.Show("Không tìm thấy kết quả!")
            listPin.Clear()
        End If
        dgvDuLieuThem.DataSource = listPin
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txtTenPin.Text = ""
        txtImp.Text = ""
        txtKichThuoc.Text = ""
        txtHangSX.Text = ""
        txtImp.Text = ""
        txtVmp.Text = ""
        txtIsc.Text = ""
        txtVoc.Text = ""
        cbLoaiPin.Text = ""
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        'Dim pin As New PinDTO()
        'pin.MaPin = BUS.PinBUS.ReadData()(dgvDuLieuThem.CurrentRow.Index).MaPin
        'pin.TenPin = txtTenPin.Text
        'pin.MaLoai = cbLoaiPin.SelectedValue
        'pin.CongSuat = txtImp.Text
        'pin.KichThuoc = txtKichThuoc.Text
        'pin.HangSanXuat = txtHangSX.Text
        'pin.Imp = txtImp.Text
        'pin.Vmp = txtVmp.Text
        'pin.Isc = txtIsc.Text
        'pin.Voc = txtVoc.Text

        'Dim delete As New PinBUS()
        'delete.DeleteData(pin.MaPin)
        'MessageBox.Show("Đã xóa dữ liệu thành công", "Thông báo", MessageBoxButtons.OK)
        'LoadData()
        Dim qs = MsgBox("Bạn chắc chắn muốn xóa tất cả thông tin ", vbInformation + vbYesNo, "Thông báo")
        For Each i As DataGridViewRow In dgvDuLieuThem.SelectedRows
            Dim j As Integer = Integer.Parse(i.Cells(0).Value.ToString())
            sListPinSelect.Add(j)
        Next
        If qs = vbYes Then
            If sListPinSelect.Count > 0 Then
                For j As Integer = 0 To sListPinSelect.Count - 1
                    pinController.DeleteData(sListPinSelect.Item(j))
                Next
                MsgBox("Dữ liệu đã xóa thành công", vbInformation, "Thông báo")
                listPin = BUS.PinBUS.ReadData()
                'LoadGrid()
                dgvDuLieuThem.DataSource = listPin
            End If
        Else
            MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        'Dim child As New frmLuuPinNLMT()
        'child.ReceiveData = Me.list
        'child.tracuu = True
        'child.ShowDialog()
        For Each i As DataGridViewRow In dgvDuLieuThem.SelectedRows
            Dim j As Integer = Integer.Parse(i.Cells(0).Value.ToString())
            sListPinSelect.Add(j)
        Next
        If sListPinSelect.Count > 0 Then
            frmLuuPinNLMT.AddList(sListPinSelect)
            frmLuuPinNLMT.MdiParent = Me.MdiParent
            frmLuuPinNLMT.Show()

            'LoadGrid()
            dgvDuLieuThem.DataSource = listPin
            sListPinSelect.Clear()
        Else
            MsgBox("Bạn chưa chọn!", vbExclamation, "Thông báo")
        End If
    End Sub

    

    Private Sub PrintDocument1_PrintPage_1(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim bm As New Bitmap(Me.dgvDuLieuThem.Width, Me.dgvDuLieuThem.Height)
        dgvDuLieuThem.DrawToBitmap(bm, New Rectangle(0, 0, Me.dgvDuLieuThem.Width, Me.dgvDuLieuThem.Height))
        e.Graphics.DrawImage(bm, 0, 0)
    End Sub

    'Private Sub dgvDuLieuThem_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellClick
    '    Dim i As Integer
    '    i = dgvDuLieuThem.CurrentRow.Index
    '    txtMaPin.Text = dgvDuLieuThem.Item(0, i).Value
    '    txtTenPin.Text = dgvDuLieuThem.Item(1, i).Value
    '    cbLoaiPin.Text = dgvDuLieuThem.Item(2, i).Value
    '    txtImp.Text = dgvDuLieuThem.Item(3, i).Value
    '    txtKichThuoc.Text = dgvDuLieuThem.Item(4, i).Value
    '    txtHangSX.Text = dgvDuLieuThem.Item(5, i).Value
    '    txtImp.Text = dgvDuLieuThem.Item(6, i).Value
    '    txtVmp.Text = dgvDuLieuThem.Item(7, i).Value
    '    txtIsc.Text = dgvDuLieuThem.Item(8, i).Value
    '    txtVoc.Text = dgvDuLieuThem.Item(9, i).Value
    'End Sub

    Private Sub cbChinhSua_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbChinhSua.CheckedChanged
        If cbChinhSua.Checked = True Then
            btnSua.Enabled = True
            btnXoa.Enabled = True
        Else
            btnSua.Enabled = False
            btnXoa.Enabled = False
        End If
    End Sub

    Dim MyDataGridViewPrinter As DataGridViewPrinter
    Private Function SetupThePrinting() As Boolean
        If PrintDialog.ShowDialog() <> DialogResult.OK Then
            Return False
        End If

        PrintDocument.DocumentName = "Các Hệ Thống Điện Năng Lượng Mặt Trời Phù Hợp"
        PrintDocument.PrinterSettings = PrintDialog.PrinterSettings
        PrintDocument.DefaultPageSettings = PrintDialog.PrinterSettings.DefaultPageSettings
        PrintDocument.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)

        If MessageBox.Show("Canh chính giữa trang?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            MyDataGridViewPrinter = New DataGridViewPrinter(dgvDuLieuThem, PrintDocument, True, True, "Các Hệ Thống Điện Năng Lượng Mặt Trời Phù Hợp", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
        Else
            MyDataGridViewPrinter = New DataGridViewPrinter(dgvDuLieuThem, PrintDocument, False, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
        End If
        Return True
    End Function

    Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
        'PrintPreviewDialog1.Document = PrintDocument1
        'PrintDocument1.DefaultPageSettings.Landscape = True
        'PrintPreviewDialog1.ShowDialog()
        If SetupThePrinting() Then
            PrintDocument.Print()
        End If

    End Sub

    Private Sub btnXemTruoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemTruoc.Click
        If SetupThePrinting() Then
            PrintPreviewDialog.Document = PrintDocument
            PrintPreviewDialog.ShowDialog()
        End If
    End Sub

    Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
        Dim more As Boolean = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If more = True Then
            e.HasMorePages = True
        End If
    End Sub

    Private Sub cbNangCao_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNangCao.CheckedChanged
        If cbNangCao.Checked = True Then
            txtImp.Enabled = True
            txtToImp.Enabled = True
            txtVmp.Enabled = True
            txtToVmp.Enabled = True
            txtIsc.Enabled = True
            txtToIsc.Enabled = True
            txtVoc.Enabled = True
            txtToVoc.Enabled = True
            txtCongSuat.Enabled = True
            txtToCongSuat.Enabled = True
        Else
            txtImp.Enabled = False
            txtToImp.Enabled = False
            txtVmp.Enabled = False
            txtToVmp.Enabled = False
            txtIsc.Enabled = False
            txtToIsc.Enabled = False
            txtVoc.Enabled = False
            txtToVoc.Enabled = False
            txtCongSuat.Enabled = False
            txtToCongSuat.Enabled = False
        End If
    End Sub

    Public Sub Export_To_Exel(ByVal _DataSet As DataTable, ByVal _List_Col As String, ByVal Path As String)
        '- _Dataset: lưu dữu liệu cần Export
        '- _List_Col: Danh sách tên cột hiển thị. Tên cột cách nhau dấu ','
        '- Path: Đường dẫn lưu file excel
        Dim col, row As Integer
        Dim arrListcol() As String
        arrListcol = _List_Col.Split(",")

        If _DataSet.Rows.Count <= 0 Then
            Exit Sub
        End If

        Dim Excel As Object = CreateObject("Excel.Application")
        If Excel Is Nothing Then
            MessageBox.Show("Để xuất ra Excel, máy bạn phải cài đặt bộ phần mềm Microsoft Office.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()

                Dim i As Integer = 2
                .cells(1, 3).value = "Date:" + Format(Now, "MMM-dd-yyyy")
                .cells(2, i).value = "No"
                .cells(2, i).EntireRow.Font.Bold = True
                For col = 0 To arrListcol.Length - 1
                    i += 1
                    .cells(2, i).value = arrListcol(col).ToUpper()
                    .cells(2, i).EntireRow.Font.Name = "Times New Roman"
                    .cells(2, i).EntireRow.Font.Size = 10
                    .cells(2, i).EntireRow.Font.Bold = True
                Next
                i = 3
                Dim k As Integer = 3

                For col = 0 To _DataSet.Columns.Count - 1
                    i = 3
                    For row = 0 To _DataSet.Rows.Count - 1
                        .Cells(i, 2).Value = i - 2
                        .Cells(i, k).Value = _DataSet.Rows(row).ItemArray(col)
                        i += 1
                    Next
                    k += 1
                Next
                .Columns.AutoFit()
                .ActiveCell.Worksheet.SaveAs(Path)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MessageBox.Show("Xuất dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Dim proc As System.Diagnostics.Process
            For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
                proc.Kill()
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnExportExcel_Click(sender As Object, e As EventArgs) Handles btnExportExcel.Click
        dataPin = New DataTable()
        dataPin.Columns.Add("MaAcQuy", GetType(String))
        dataPin.Columns.Add("TenAcQuy", GetType(String))
        dataPin.Columns.Add("HangSanXuat", GetType(String))
        dataPin.Columns.Add("TenLoai", GetType(String))
        dataPin.Columns.Add("DongDien", GetType(String))
        dataPin.Columns.Add("DienAp", GetType(String))
        dataPin.Columns.Add("Gia", GetType(String))
        For i As Integer = 0 To Me.dgvDuLieuThem.RowCount - 1
            Dim dr As DataRow = dataPin.NewRow
            Dim hoangvu As String = dgvDuLieuThem.Rows.Item(i).Cells(2).Value.ToString
            dr("MaAcQuy") = dgvDuLieuThem.Rows.Item(i).Cells(0).Value.ToString
            dr("TenAcQuy") = dgvDuLieuThem.Rows.Item(i).Cells(1).Value.ToString
            dr("TenLoai") = dgvDuLieuThem.Rows.Item(i).Cells(3).Value.ToString
            dr("HangSanXuat") = dgvDuLieuThem.Rows.Item(i).Cells(2).Value.ToString
            dr("DongDien") = dgvDuLieuThem.Rows.Item(i).Cells(5).Value.ToString
            dr("DienAp") = dgvDuLieuThem.Rows.Item(i).Cells(6).Value.ToString
            dr("Gia") = dgvDuLieuThem.Rows.Item(i).Cells(7).Value.ToString
            dataPin.Rows.Add(dr)
        Next
        Try
            SaveFileExcel.Filter = "Excel (*.xls)|*.xls"
            SaveFileExcel.ShowDialog()
            Dim File_name = SaveFileExcel.FileName
            Export_To_Exel(dataPin, "Mã Pin,Tên Pin,Hãng Sản Xuất,Loại,Dòng Điện, Điện Áp,Giá", File_name)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End Try
    End Sub
End Class